// 导入createStore方法
import { createStore } from 'redux'

// 初始化数据
const initValue = {
    apple: 0,
    banana: 0,
    orange: 0
}

// 创建 reducer
const reducer = (state, action) => {
    switch (action.type) {
        case 'add':
            return { ...state, [action.name]: state[action.name] + 1 }

        case 'sub':
            let num = state[action.name]
            if (num <= 0) return { ...state }
            return { ...state, [action.name]: state[action.name] - 1 }

        default:
            return state
    }
}


// 调用createStore方法，生成store仓库
const store = createStore(reducer, initValue)

// 导出store
export default store